import { _decorator, assetManager, AssetManager, Component, instantiate, Node, Prefab, Quat, Vec3 } from 'cc';
import { BoardModel } from '../BoardModel/BoardModel';
import { GameItemID } from '../GameItemID/GameItemID';
import { NailBase } from '../Nail/NailBase';
import { NailColorType } from '../CustomModel';
const { ccclass, property } = _decorator;

/**
 * 
 */
@ccclass('BoardBase')
export class BoardBase extends Component {

    @property(Node)
    boardModels: Node = null

    @property({
        type: Node,
        tooltip: "钉子父节点"
    })
    nails: Node = null

    @property({
        type: Node,
        tooltip: "钉子位置父节点"
    })
    nailsPositionParent: Node = null

    @property(Prefab)
    nailPrefab: Prefab = null

    private _nailsCom_map: Map<number, NailBase> = new Map()

    public get itemID(): number {
        return this._itemID
    }
    private set itemID(id: number) {
        this._itemID = id
    }
    private _itemID: number = -1


    start() {
        this.init()

        this.scheduleOnce(() => {
            this.addNails([
                0, 0, 0,
                1, 1, 1,
                2, 2, 2,
    
                0, 0, 0,
                1, 1, 1,
                2, 2, 2
            ])
        }, 1)
       
    }

    private init() {
        this.initData()
        this.initEvent()
    }
    private initEvent() {

    }
    private initData() {
        this._itemID = GameItemID.generateBoardID()
        const models_n = this.boardModels.children.length
        if (!models_n) {
            console.error("没添加模型")
        }
    }

    private removeNail(nail_node_: Node, itemID_: number) {
        if (this._nailsCom_map.has(itemID_)) {
            this._nailsCom_map.delete(itemID_)
            if (this._nailsCom_map.size == 0) {
                this.fallModel()
            }
        }
    }

    private fallModel() {
        this.boardModels.children.forEach((nd_: Node) => {
            nd_.getComponent(BoardModel).fall()
        })
    }

    /**
     * 获取钉子的数量
     * @returns 
     */
    public nailsCount(): number {
        return this.nailsPositionParent.children.length;
    }

    public addNails(nails_array_: NailColorType[]) {
        nails_array_.forEach((color_: NailColorType, index_: number) => {
            console.log("index_", index_, this.nailsPositionParent.children)
            const pos_nd = this.nailsPositionParent.children[index_]
            const worldPos = pos_nd.getWorldPosition().clone()
            const worldRotation =  pos_nd.getWorldRotation().clone()
            this.addNail(color_, worldPos, worldRotation)
        })
    }

    private addNail(nailColor_nct_: NailColorType, worldPosition_: Vec3, worldRotation_: Quat) {
        const nail_node = instantiate(this.nailPrefab)
        nail_node.setWorldPosition(worldPosition_)
        nail_node.setWorldRotation(worldRotation_)
        this.nails.addChild(nail_node)
       

        const nail_com = nail_node.getComponent(NailBase)
        nail_com.init({ color: nailColor_nct_ })
        nail_com.onCanPull = this.removeNail.bind(this)
        this._nailsCom_map.set(nail_com.itemID, nail_com)
    }
}